package com.example.demo.service.impl;

import com.example.demo.beans.entity.DemoSysMenu;
import com.example.demo.beans.entity.DemoSysMenuExample;
import com.example.demo.dao.mapper.CommMapper;
import com.example.demo.dao.mapper.DemoSysMenuMapper;
import com.example.demo.service.MenuService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Log4j2
public class MenuServiceImpl implements MenuService {
    @Autowired
    DemoSysMenuMapper sysMenuMapper;

    @Autowired
    CommMapper commMapper;

    @Override
    public int addOrUpdateDemoSysMenu(DemoSysMenu sysMenu) {
        if(sysMenu.getF_ModuleId() != null && sysMenu.getF_ModuleId() > 0){
            sysMenu.setUpdatetimestamp(new Date());
            return sysMenuMapper.updateByPrimaryKey(sysMenu);
        }
        else {
            sysMenu.setF_ModuleId(commMapper.getObjid());
            sysMenu.setCreatetimestamp(new Date());
            sysMenu.setUpdatetimestamp(new Date());
            return sysMenuMapper.insertSelective(sysMenu);
        }
    }

    @Override
    public List<DemoSysMenu> getAllMenus() {
        DemoSysMenuExample example = new DemoSysMenuExample();
        return sysMenuMapper.selectByExample(example);
    }
}
